import SettingsData from "../data/SettingsData"
import { TourTransitionType } from "../enum/tourTransition.enum"
import TourTransitionFactory from "./TourTransitionFactory"

export default class TourZoomTransition {
  settingsData: SettingsData
  zoom: any
  stopZooming: any
  type: number
  toIndex: number
  currentTransitionPromise: any
  onStopRequested: () => Promise<void>
  constructor(e, t, i) {
    this.settingsData = e
    this.zoom = t
    this.stopZooming = i
    this.type = TourTransitionType.Zoom
    this.toIndex = -1
    this.currentTransitionPromise = null
    this.onStopRequested = () => Promise.resolve()
  }
  get active() {
    return null !== this.currentTransitionPromise
  }
  get promise() {
    return this.currentTransitionPromise ? this.currentTransitionPromise : Promise.resolve()
  }
  async stop() {
    this.currentTransitionPromise && (await this.onStopRequested(), await this.promise, (this.currentTransitionPromise = null))
  }
  start() {
    if (this.active) throw Error("Transition already active")
    const e = TourTransitionFactory.getZoomDuration(this.settingsData)
    this.currentTransitionPromise = this.zoom(e, -0.0005).then(() => {
      this.currentTransitionPromise = null
    })
    this.onStopRequested = async () => {
      await this.stopZooming()
    }
    return this
  }
}
